package com.sino.business.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.sino.bean.Entityrolerelation;
import com.sino.bean.Qxkw;
import com.sino.bean.Role;
import com.sino.bean.TblZhuanjiarukuxinxibiao;
import com.sino.bean.Users;
import com.sino.business.IRoleService;
import com.sino.dao.IAclDAO;
import com.sino.dao.IPatentManageDAO;
import com.sino.dao.IRoleDAO;
import com.sino.model.EntityrolerelationModel;
import com.sino.model.RoleModel;
import com.sino.model.StaticModel;

public class RoleServiceImp implements IRoleService {
	private IRoleDAO iroleDAO;
	private IAclDAO aclDAO;
	private IPatentManageDAO patentManageDAO;

	/**
	 * @param iroleDAO
	 *            the iroleDAO to set
	 */
	public void setIroleDAO(IRoleDAO iroleDAO) {
		this.iroleDAO = iroleDAO;
	}

	/**
	 * @param patentManageDAO
	 *            the patentManageDAO to set
	 */
	public void setPatentManageDAO(IPatentManageDAO patentManageDAO) {
		this.patentManageDAO = patentManageDAO;
	}

	
	/**
	 * @param aclDAO the aclDAO to set
	 */
	public void setAclDAO(IAclDAO aclDAO) {
		this.aclDAO = aclDAO;
	}

	public int delRoleModel(int id) {
		if (null != this.listEntityByRoleAndType(id, 1)&&this.listEntityByRoleAndType(id, 1).size()>0) {
			return 1;
		} else if (null != this.listEntityByRoleAndType(id, 0)&&this.listEntityByRoleAndType(id, 0).size()>0) {
			return 0;
		}
		else if(null!=this.aclDAO.listModuleByRole(id)&&this.aclDAO.listModuleByRole(id).size()>0)
		{
			return 2;
		}
		this.iroleDAO.delRole(id);
		return -1;
	}

	public List<RoleModel> listAllRoleModels() {
		// TODO Auto-generated method stub
		List<Role> list = this.iroleDAO.listAllRoles();
		List<RoleModel> ls = new ArrayList<RoleModel>();
		for (Role r : list) {
			RoleModel rm = this.getRoleModel(r);
			ls.add(rm);

		}
		return ls;
	}

	public void saveRoleModel(RoleModel roleModel) {
		this.iroleDAO.saveRole(this.getRole(roleModel));
	}

	public void updateRoleModel(RoleModel roleModel) {
		this.iroleDAO.updateRole(this.getRole(roleModel));

	}

	public List<StaticModel> listAllExperts() {
		List<TblZhuanjiarukuxinxibiao> tzjList = this.iroleDAO.listAllExperts();
		List<StaticModel> smList = new ArrayList<StaticModel>();
		for (TblZhuanjiarukuxinxibiao tzj : tzjList) {
			StaticModel sm = new StaticModel();
			sm = this.getStaticModel(tzj);
			smList.add(sm);
		}
		return smList;
	}

	public List<StaticModel> listAllUsers() {
		List<Users> userList = this.iroleDAO.listAllUsers();
		List<StaticModel> smList = new ArrayList<StaticModel>();
		for (Users user : userList) {
			StaticModel sm = new StaticModel();
			sm = this.getStaticModel(user);
			smList.add(sm);
		}
		return smList;
	}

	public String listUserString(int roleId) {
		// List<Users> userList = this.iroleDAO.listAllUsers();
		// List<StaticModel> smList = new ArrayList<StaticModel>();
		// for (Users user : userList) {
		// StaticModel sm = new StaticModel();
		// sm = this.getStaticModel(user);
		// smList.add(sm);
		// }
		// 获取区县科委的类型
		List<Qxkw> qxkwTypeList = (List<Qxkw>) this.patentManageDAO
				.findQxkwType("type");
		// 对北京市发明专利奖励办公室构造一个区县科委类型
		Qxkw virtualQxkwType = new Qxkw();
		virtualQxkwType.setId("00");
		virtualQxkwType.setName("奖励办公室");
		qxkwTypeList.add(virtualQxkwType);// 将构造的类型添加到类型列表中
		List<Entityrolerelation> list = this.iroleDAO.listEntityByRoleAndType(
				roleId, 1);// 获取一个角色被授予的用户
		List<Integer> userIdList = new ArrayList<Integer>();// 构造一个用户id的列表，用于后续的判断
		for (Entityrolerelation err : list) {
			userIdList.add(err.getEntityId());// 把实体ID放到userIdList
		}
		// 声明变量
		List<Qxkw> qxkwList;// 区县集合
		List<Users> user;// 用户集合
		StringBuffer sb = new StringBuffer("");

		for (Qxkw qxkwType : qxkwTypeList) {
			sb.append("<tr><td bgcolor='#CCCC00' class='font__12_grey'>");
			sb.append("<input type='checkbox' id='" + qxkwType.getId()
					+ "' qxkwId='" + 0 + "' qxkwTypeId='" + 0
					+ "' name='qxkwType'/>");
			sb.append(qxkwType.getName());
			sb.append("</td></tr>");
			// System.out.println("sssss:" + qxkwType.getId() + ":"
			// + qxkwType.getName());
			qxkwList = this.patentManageDAO.listQxkwByIdLike(qxkwType.getId());
			for (Qxkw qxkw : qxkwList) {
				sb.append("<tr><td bgcolor='#CC6666' class='font__12_grey'>");
				sb.append("&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' id='"
						+ qxkw.getId() + "' qxkwId='" + 0 + "' qxkwTypeId='"
						+ qxkwType.getId() + "' name='qxkw'/>");
				sb.append(qxkw.getName());
				sb.append("</td></tr>");
				// System.out.println(" "
				// + qxkw.getName());
				user = this.iroleDAO.listUsersByQxkw(qxkw.getId());
				if (null != user) {
					for (Users u : user) {
						if (isExitInArray(u.getId(), userIdList) == true) {
							sb
									.append("<tr><td bgcolor='#EAEEEE' class='font__12_grey'>");
							sb
									.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' id='"
											+ u.getId()
											+ "' name='authList' qxkwId='"
											+ qxkw.getId()
											+ "' qxkwTypeId='"
											+ qxkwType.getId()
											+ "' value='"
											+ u.getId()
											+ "' checked='checked'/>");
							sb.append(u.getName());
							sb.append("</td></tr>");
						} else {
							sb
									.append("<tr><td bgcolor='#EAEEEE' class='font__12_grey'>");
							sb
									.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' id='"
											+ u.getId()
											+ "' name='authList' qxkwId='"
											+ qxkw.getId()
											+ "' qxkwTypeId='"
											+ qxkwType.getId()
											+ "' value='"
											+ u.getId() + "'/>");
							sb.append(u.getName());
							sb.append("</td></tr>");
						}
						// System.out
						// .println(" user:"
						// + u.getName());
					}
				}
			}
		}
		return sb.toString();
	}

	// 由bean转成model
	public RoleModel getRoleModel(Role role) {
		RoleModel rm = new RoleModel();
		rm.setId(role.getId());
		rm.setName(role.getName());
		return rm;
	}

	public Role getRole(RoleModel rm) {
		Role role = new Role();
		role.setId(rm.getId());
		role.setName(rm.getName());
		return role;
	}

	public EntityrolerelationModel getEntityrolerelationModel(
			Entityrolerelation err) {
		EntityrolerelationModel errm = new EntityrolerelationModel();
		errm.setId(err.getId());
		errm.setEntityId(err.getEntityId());
		errm.setType(err.getType());
		errm.setRole(this.getRoleModel(err.getRole()));
		return errm;
	}

	public Entityrolerelation getEntityrolerelation(EntityrolerelationModel errm) {
		Entityrolerelation err = new Entityrolerelation();
		err.setId(errm.getId());
		err.setEntityId(errm.getEntityId());
		err.setType(errm.getType());
		err.setRole(this.getRole(errm.getRole()));
		return err;
	}

	public StaticModel getStaticModel(Users users) {
		StaticModel sm = new StaticModel();
		sm.setId(users.getId().toString());
		sm.setName(users.getName() + "(" + users.getTruename() + ")");
		return sm;
	}

	public StaticModel getStaticModel(TblZhuanjiarukuxinxibiao tzj) {
		StaticModel sm = new StaticModel();
		sm.setId(tzj.getFBh().toString());
		sm.setName(tzj.getXm());
		return sm;
	}

	public List<StaticModel> listEntityByRoleAndType(int roleId, int type) {
		List<Entityrolerelation> list = this.iroleDAO.listEntityByRoleAndType(
				roleId, type);
		List<StaticModel> ls = new ArrayList<StaticModel>();
		if (1 == type) {
			for (Entityrolerelation err : list) {
				StaticModel sm = this.getStaticModel(this.iroleDAO
						.getUserById(err.getEntityId()));
				ls.add(sm);
			}
		} else if (0 == type) {
			for (Entityrolerelation err : list) {
				StaticModel sm = this.getStaticModel(this.iroleDAO
						.getExpertById(err.getEntityId()));
				ls.add(sm);
			}
		}
		return ls;
	}

	public List<StaticModel> listUnAuthEntity(int roleId, int type) {
		List<StaticModel> authEntityList = this.listEntityByRoleAndType(roleId,
				type);
		List<StaticModel> allEntityList = null;
		String temp;
		if (1 == type)// 当实体是用户的情况下
		{
			allEntityList = this.listAllUsers();
		} else if (0 == type)// 当实体是专家的情况下
		{
			allEntityList = this.listAllExperts();
		}
		if (null != allEntityList && null != authEntityList) {
			for (StaticModel errm : authEntityList) {
				temp = errm.getId();
				Iterator it = allEntityList.iterator();
				while (it.hasNext()) {
					StaticModel sm = (StaticModel) it.next();
					if (sm.getId().equals(temp))
						it.remove();
					continue;
				}
			}
		}
		return allEntityList;
	}

	public List<StaticModel> listAllExpertForAuth(int roleId) {
		List<Entityrolerelation> errList = this.iroleDAO
				.listEntityByRoleAndType(roleId,
						EntityrolerelationModel.ENTITY_EXPERT);
		List<Integer> entityIdList = new ArrayList<Integer>();
		for (Entityrolerelation err : errList) {
			entityIdList.add(err.getEntityId());
		}
		List<StaticModel> allEntityList = this.listAllExperts();
		for (StaticModel sm : allEntityList) {
			if (this.isExitInArray(new Integer(sm.getId()), entityIdList)) {
				sm.setFlag(true);
			}
		}
		return allEntityList;
	}

	public void saveOrRevokeEntity(int roleId, int type, String[] entityList) {

		List<Entityrolerelation> ls = this.iroleDAO.listEntityByRoleAndType(
				roleId, type);
		if (!ls.isEmpty() && ls.size() > 0) {
			for (Entityrolerelation err : ls) {
				this.iroleDAO.delEntityrolerelation(err);
			}
		}
		if (null != entityList && entityList.length > 0) {
			for (int i = 0; i < entityList.length; i++) {
				Entityrolerelation err = new Entityrolerelation();
				err.setType(type);
				err.setEntityId(new Integer(entityList[i]));
				Role role = this.iroleDAO.getRole(roleId);
				err.setRole(role);
				this.iroleDAO.saveEntityrolerelation(err);
			}
		}
	}

	/**
	 * 判断一个整数data是否在整数列表datas中 如果在则返回true，否则返回false
	 * 
	 * @param data
	 * @param datas
	 * @return
	 */
	public boolean isExitInArray(int data, List<Integer> datas) {
		boolean bl = false;
		for (Integer i : datas) {
			if (data == i) {
				bl = true;
				break;
			}
		}
		return bl;
	}

}
